<template>
  <el-dialog :title="showTitle" :visible="showDialog" @close="close">
    <el-form ref="form" label-width="120px" :rules="rules" :model="formData">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="formData.name" style="width: 80%" placeholder="2-10个字符" size="small" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="formData.code" style="width: 80%" placeholder="2-10个字符" size="small" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="managerId">
        <el-select v-model="formData.managerId" style="width: 80%" placeholder="请选择负责人" size="small">
          <el-option v-for="item in managerList" :key="item.id" :label="item.username" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="formData.introduce" type="textarea" style="width: 80%" placeholder="1-100个字符" size="small" :rows="4" />
      </el-form-item>
      <el-form-item>
        <el-row type="flex" justify="center">
          <el-col :span="12">
            <el-button type="primary" size="mini" @click="btnOk">确定</el-button>
            <el-button size="mini" @click="close">取消</el-button>
          </el-col>
        </el-row>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import { getDepartment, getManagerList, addDepartment, getCompanyDetail, updateCompanyDetail } from '@/api/department'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    currentNode: {
      type: Number,
      default: null
    }
  },
  data() {
    return {
      formData: {
        code: '',
        introduce: '',
        managerId: '',
        name: '',
        pid: ''
      },
      rules: {
        code: [{
          required: true, message: '部门编码不能为空', trigger: 'blur'
        }, {
          min: 2,
          max: 10,
          message: '部门编码为2-10位字符',
          trigger: 'blur'
        }, {
          trigger: 'blur',
          validator: async(rule, value, callback) => {
            let res = await getDepartment()
            if (this.formData.id) {
              res = res.filter(item => item.id !== this.formData.id)
            }
            if (res.some(item => item.code === value)) {
              callback(new Error('部门编码已经存在'))
            } else {
              callback()
            }
          }
        }],
        introduce: [{
          required: true, message: '部门介绍不能为空', trigger: 'blur'
        }, {
          min: 1,
          max: 100,
          message: '部门介绍为1-100位字符',
          trigger: 'blur'
        }],
        managerId: [{
          required: true, message: '部门负责人不能为空', trigger: 'blur'
        }],
        name: [{
          required: true, message: '部门名称不能为空', trigger: 'blur'
        }, {
          min: 2,
          max: 10,
          trigger: 'blur',
          message: '部门名称为2-10位字符'
        }, {
          trigger: 'blur',
          validator: async(rule, value, callback) => {
            let res = await getDepartment()
            if (this.formData.id) {
              res = res.filter(item => item.id !== this.formData.id)
            }
            if (res.some(item => item.code === value)) {
              callback(new Error('部门编码已经存在'))
            } else {
              callback()
            }
          }
        }]
      },
      managerList: []
    }
  },
  computed: {
    showTitle() {
      return this.formData.id ? '编辑部门' : '新增部门'
    }
  },
  created() {
    this.getManagerList()
  },
  methods: {
    close() {
      this.formData = {
        code: '',
        introduce: '',
        managerId: '',
        name: '',
        pid: ''
      }
      this.$refs.form.resetFields()
      this.$emit('update:showDialog', false)
    },
    async getManagerList() {
      const res = await getManagerList()
      this.managerList = res
    },
    btnOk() {
      this.$refs.form.validate(async isOk => {
        if (isOk) {
          let msg = '新增'
          if (this.formData.id) {
            await updateCompanyDetail(this.formData)
            msg = '更新'
          } else {
            await addDepartment({ ...this.formData, pid: this.currentNode })
          }
          this.$emit('updateDepartment')
          this.$message.success(`${msg}部门成功`)
          this.close()
        }
      })
    },
    async getCompanyDetail() {
      const res = await getCompanyDetail(this.currentNode)
      this.formData = res
    }
  }
}
</script>

<style>

</style>
